package com.scut.dispatching_server.service;

import com.scut.dispatching_server.config.GlobalVariables;
import com.scut.dispatching_server.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Set;

@Component
public class RelationshipCache {
    @Autowired
    private RedisUtil redisUtil;

    public ArrayList<String> getRelationships() {
        ArrayList<String> relationships = new ArrayList<>();
        Set<Object> rawRelationships = redisUtil.sGet(GlobalVariables.ALIVE_RELATIONSHIPS_SET_KEY);
        for(Object relationship : rawRelationships) {
            relationships.add((String)relationship);
        }
        return relationships;
    }

    public long addRelationship(String relationship) {
        return redisUtil.sSet(GlobalVariables.ALIVE_RELATIONSHIPS_SET_KEY, relationship);
    }

    public void clearRelationships() {
        redisUtil.del(GlobalVariables.ALIVE_RELATIONSHIPS_SET_KEY);
    }

    public long deleteRelationship(String relationship){
        return redisUtil.setRemove(GlobalVariables.ALIVE_RELATIONSHIPS_SET_KEY, relationship);
    }
}
